import dht, machine, urequests, time
from umqtt.simple import MQTTClient
time.sleep(1)
try:
	d=dht.DHT11(machine.Pin(14))
	c = MQTTClient("1cb100", 'broker.mqttdashboard.com',1883)
	c.connect()
except:
	print('Main Start Error')
	time.sleep(2)
	machine.reset()
print('Main Start')

while True:
	d.measure()
	try:
		print('https://api.thingspeak.com/update?api_key=TRAQ672LLQ3M7XJS&field1=%d&field2=%d'%(d.temperature(),d.humidity()))
		r=urequests.get( 'https://api.thingspeak.com/update?api_key=TRAQ672LLQ3M7XJS&field1=%d&field2=%d'%(d.temperature(),d.humidity()))
		print(r.text)
		r.close()

	except :
		r.close()
		print('Requests Error')
		time.sleep(2)
		machine.reset()
	for i in range(6):
		time.sleep(5)
		d.measure()
		print('field1=%d&field2=%d'%(d.temperature(),d.humidity()))
		c.publish(b"suzuki/temp", b"%d"%d.temperature())
		c.publish(b"suzuki/humidity", b"%d"%d.humidity())

		print('MQTT OK')

